package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.vo.CategoryBrandVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "品牌与商品分类关联信息接口管理")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @RequiresPermissions("product:categoryBrand:list")
    @Operation(summary = "查询分类品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(CategoryBrand categoryBrand) {
        startPage();
        List<CategoryBrandVo> list = categoryBrandService.selectList(categoryBrand);
        return getDataTable(list);
    }

    @RequiresPermissions("product:categoryBrand:query")
    @Operation(summary = "获取分类品牌详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CategoryBrandVo vo = categoryBrandService.getInfo(id);
        return success(vo);
    }

    @Log(title = "分类品牌", businessType = BusinessType.INSERT)
    @RequiresPermissions("product:categoryBrand:add")
    @Operation(summary = "新增分类品牌关联关系")
    @PostMapping
    public AjaxResult save(@RequestBody @Validated CategoryBrand categoryBrand) {
        categoryBrandService.save(categoryBrand);
        return success();
    }

    @Log(title = "分类品牌", businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:categoryBrand:edit")
    @Operation(summary = "修改分类品牌关联关系")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated CategoryBrand categoryBrand) {
        categoryBrandService.update(categoryBrand);
        return success();
    }

    @Log(title = "分类品牌", businessType = BusinessType.DELETE)
    @RequiresPermissions("product:categoryBrand:remove")
    @Operation(summary = "删除分类品牌关联关系")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") Long[] ids) {
        categoryBrandService.delete(ids);
        return success();
    }

    @RequiresPermissions("product:categoryBrand:query")
    @Operation(summary = "根据分类id获取品牌列表")
    @GetMapping("/brandList/{categoryId}")
    public AjaxResult selectBrandListByCategoryId(@PathVariable Long categoryId) {
        List<Brand> list = categoryBrandService.selectBrandListByCategoryId(categoryId);
        return success(list);
    }
}
